Import the GIS module and other needed Python modules

The IPython.display module has some helper functions that the Python API takes advantage of for displaying objects like item details and maps in the notebook.


In [ ]:
from arcgis.gis import GIS
from getpass import getpass
from IPython.display import display
Create the GIS object and point it to AGOL

In [ ]:
# Get username and password
username = input('Username: ')
password = getpass(prompt='Password: ')

In [ ]:
# Connect to portal
gis = GIS("https://arcgis.com/", username, password)
Test the connection

The output here is an example of the Python API taking advantage of IPython.display.


In [ ]:
user = gis.users.get(username)
user
Get the item that you want to update

Portals allows users to store and share a variety of items. Each item has a type, such as Web Map or Feature Service, and a set of type keywords that provide additional information on the characteristics of the type.

See Items and item types for more information.


In [ ]:
title = input("Feature class to search for: ")

In [ ]:
items = gis.content.search(query="title:'" + title + "' AND owner:" + username, item_type="Feature Service")
print(type(items), len(items))
print(type(items[0]))

In [ ]:
item = items[0]
item

In [ ]:
item.tags
Update the metadata

In [ ]:
# First set up some variables for input ot the *update* method.
thumbnail_path = "c:/temp/Hospitals.JPG"
tags = list(item.tags)
tags.append("health")
item_properties = {"snippet": "Location of Cambridge hospitals.",
                   "title": "Cambridge Hospitals",
                   "tags": ','.join(tags),
                   "accessinformation": "City of Cambridge GIS",
                   "licenseInfo": "License Info"
}

In [ ]:
# Then perform the update
item.update(item_properties, thumbnail=thumbnail_path)
item

In [ ]:
# Get the updated *item*
items[0].tags
Final code

Removing the display code and reducing the above to just what's needed to update the metadata shows that the Python API requires very little code to update an item's metadata. Error handling would need to be added for production, but that is required whether you use the Python API or interact with the REST API directly.

# ##### Import the GIS module and other needed Python modules
from arcgis.gis import GIS
from getpass import getpass

# ##### Create the GIS object and point it to AGOL
# Get username and password
username = input('Username: ')
password = getpass(prompt='Password: ')

# Connect to portal
gis = GIS("https://arcgis.com/", username, password)


# ##### Get the item that you want to update
title = input("Feature class to search for: ")

items = gis.content.search(query="title:'" + title + "' AND owner:" + username, item_type="Feature Service")
item = items[0]

# ##### Update the metadata
# First set up some variables for input ot the *update* method.
thumbnail_path = "c:/temp/Hospitals.JPG"
tags = list(item.tags)
tags.append("health")
item_properties = {"snippet": "Location of Cambridge hospitals.",
                   "title": "Cambridge Hospitals",
                   "tags": ','.join(tags),
                   "accessinformation": "City of Cambridge GIS",
                   "licenseInfo": "License Info"
}


# Then perform the update
item.update(item_properties, thumbnail=thumbnail_path)